/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.model.iva;

import java.util.Calendar;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * 'BollatoIvaTest' class: classe di test unitario per l'OUT {@link org.erossi.pigest.model.iva.BollatoIva} 
 *
 * @author erossi
 * @version Mar 1, 2009
 */
public class BollatoIvaTest extends TestCase {
  
  private static final Boolean TEST_VENDITE = true;
  private static final Boolean TEST_ACQUISTI = false;
  
  private String timestamp;

  private BollatoIva bollatoIva;
  
  private XmlWebApplicationContext apc;

  /**
   * Crea una nuova istanza della classe
   */
  public BollatoIvaTest() {
    super();

    timestamp = null;

    bollatoIva = null;

    apc = null;
  }

  /**
   * Prepara l'ambiente per l'esecuzione del test.
   * 
   * @throws java.lang.Exception eccezioni non gestite
   */
  @Before
  public void setUp() throws Exception {
    
    MockServletContext mockServletContext = new MockServletContext(".", new FileSystemResourceLoader());
    MockHttpSession mockHttpSession = new MockHttpSession(mockServletContext);
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(mockServletContext);
    mockHttpServletRequest.setSession(mockHttpSession);
    
    ServletRequestAttributes sra = new ServletRequestAttributes(mockHttpServletRequest);
    RequestContextHolder.setRequestAttributes(sra);
    
    apc = new XmlWebApplicationContext();
    apc.setServletContext(mockServletContext);
    apc.setConfigLocations(new String[] {"web/WEB-INF/spring-servlet.xml"});
    apc.refresh();

    timestamp = String.format("%1$tM%1$tS%1$tL", Calendar.getInstance());

    bollatoIva = (BollatoIva) apc.getBean("bollatoIva");
    bollatoIva.setCodice("CODICE_" + timestamp);
    bollatoIva.setDescrizione("DESCRIZIONE_" + timestamp);
    bollatoIva.setDataInizioValidita(Calendar.getInstance());
    bollatoIva.setDataFineValidita(Calendar.getInstance());
    bollatoIva.setVendite(BollatoIvaTest.TEST_VENDITE);
    bollatoIva.setAcquisti(BollatoIvaTest.TEST_ACQUISTI);
    
  }
  
  /**
   * Metodo di test per {@link org.erossi.pigest.model.iva.BollatoIva#equals(Object)}.
   */
  @Test
  public void testEquals() {
    
    BollatoIva b1 = (BollatoIva) apc.getBean("bollatoIva");
    BollatoIva b2 = (BollatoIva) apc.getBean("bollatoIva");
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));

    b1.setCodice(bollatoIva.getCodice());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setCodice(bollatoIva.getCodice());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
    
    b1.setDescrizione(bollatoIva.getDescrizione());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setDescrizione(bollatoIva.getDescrizione());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
    
    b1.setDataInizioValidita(bollatoIva.getDataInizioValidita());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setDataInizioValidita(bollatoIva.getDataInizioValidita());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
  
    b1.setDataFineValidita(bollatoIva.getDataFineValidita());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setDataFineValidita(bollatoIva.getDataFineValidita());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
    
    b1.setVendite(bollatoIva.getVendite());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setVendite(bollatoIva.getVendite());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
    
    b1.setAcquisti(bollatoIva.getAcquisti());
    assertFalse(b1.equals(b2));
    assertFalse(b2.equals(b1));
    b2.setAcquisti(bollatoIva.getAcquisti());
    assertTrue(b1.equals(b2));
    assertTrue(b2.equals(b1));
    
  }
  
  /**
   * Ripulisce l'ambiente di test dopo l'esecuzione di ogni singolo metodo.
   * 
   * @throws java.lang.Exception eccezioni non gestite
   */
  @After
  public void tearDown() throws Exception {
    apc.close();
  }

}

